package com.twu.biblioteca.main;

import com.twu.biblioteca.util.InputOutputHandler;

public class BibliotecaApp {

    private Library bangalorePublicLibrary;


    BibliotecaApp()
    {
        bangalorePublicLibrary = Library.build();
        showGreetingMessage();

    }

    public static void main(String[] args) {
        BibliotecaApp application = new BibliotecaApp();

        while(true)
        {
            application.presentOptionsMenu();

            int selectedOption = InputOutputHandler.getInt();
            String response = application.respondToUser(selectedOption);

            InputOutputHandler.display(response);
        }
    }

    private void showGreetingMessage()
    {
        InputOutputHandler.display("Welcome to Bangalore Public Library Management System!");
    }

    private void presentOptionsMenu()
    {
        Librarian librarian = bangalorePublicLibrary.getLibrarian();

        String menu = "\n=======Menu======";
        if(librarian.isUserLoggedIn())
            menu += "(Logged in as " + librarian.getLoggedInUser().getUserName() + ")\n";
        menu += "\n1. View Books";
        menu += "\n2. View Movies";
        menu += "\n3. Login";
        menu += "\n4. Reserve a Book";
        menu += "\n5. View Membership Details";
        menu += "\n6. Exit";
        menu += "\nPick an Option:";
        InputOutputHandler.display(menu);
    }

    private String respondToUser(int selectedOption)
    {
        Librarian librarian = bangalorePublicLibrary.getLibrarian();


        switch(selectedOption)
        {
            case OPTION_LIST_BOOKS:
                return librarian.listBooks();

            case OPTION_VIEW_MOVIE_LIST:
                 return librarian.listMovies();

            case OPTION_LOGIN:
                 InputOutputHandler.display("Enter UserName: ");
                 String userName = InputOutputHandler.getString();
                 InputOutputHandler.display("Enter Password: ");
                 String password = InputOutputHandler.getString();
                 String.valueOf(librarian.authenticate(userName,password));
                 break;
            case OPTION_RESERVE_BOOK:

                if(librarian.isUserLoggedIn() == false)
                    return "Please Login";

                InputOutputHandler.display("Please Enter Book Name or ISBN : ");
                String searchText = InputOutputHandler.getString();
                return librarian.reserveBook(searchText);

            case OPTION_VIEW_MEMBERSHIP:
                return librarian.viewMembership();

            case OPTION_EXIT:
                System.exit(0);

            default:
                return "Select a valid option!!";
        }
        return "";
    }



    private static final int OPTION_LIST_BOOKS = 1;
    private static final int OPTION_VIEW_MOVIE_LIST =2;
    private static final int OPTION_LOGIN = 3;
    private static final int OPTION_RESERVE_BOOK = 4;
    private static final int OPTION_VIEW_MEMBERSHIP = 5;
    private static final int OPTION_EXIT = 6;

}
